package com.yiv.yivbackend.controller;

import com.yiv.yivbackend.model.domain.User;
import com.yiv.yivbackend.model.domain.request.UserLoginRequest;
import com.yiv.yivbackend.model.domain.request.UserRrgistrationRequest;
import com.yiv.yivbackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Yivi
 *
 * 用户接口
 *
 */
@RestController //用于返回值默认为json类型
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/registration")
    public Long userRegistration(@RequestBody UserRrgistrationRequest userRrgistrationRequest){
//        如果前端传来的请求为空
        if(userRrgistrationRequest == null){
            return null;
        }
        String userAccount = userRrgistrationRequest.getUserAccount();
        String userPassword = userRrgistrationRequest.getUserPassword();
        String checkPassword = userRrgistrationRequest.getCheckPassword();
//        对前端请求数据做一个校验
        if(StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)){
            return null;
        }
        return userService.userRegistration(userAccount, userPassword, checkPassword);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest , HttpServletRequest request){
        //        如果前端传来的请求为空
        if(userLoginRequest == null){
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
//        对前端请求数据做一个校验
        if(StringUtils.isAnyBlank(userAccount, userPassword)){
            return null;
        }
        return userService.userLogin(userAccount, userPassword,request);
    }


}
